package com.bfl.sa.showcase.parentchild.service;

import com.bfl.sa.common.mybatis.entity.Query;
import com.bfl.sa.common.service.BaseService;
import com.bfl.sa.showcase.parentchild.entity.Child;
import com.bfl.sa.showcase.parentchild.entity.Parent;
import com.bfl.sa.showcase.parentchild.mapper.ChildMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class ChildService extends BaseService<Child, Long> {

    @Resource
    private ChildMapper childMapper;

    public Page<Child> findByParent(Parent parent, Pageable pageable) {
        Query query = Query.newQuery(pageable);
        query.put("parent", parent);
        return PageHelper.startPage(query).doSelectPage(() -> childMapper.findByParent(query));
    }

    Page<Child> findByParents(List<Parent> parents, Pageable pageable) {
        Query query = Query.newQuery(pageable);
        query.put("parents", parents);
        return PageHelper.startPage(query).doSelectPage(() -> childMapper.findByParents(query));
    }

    public void deleteByParent(Parent parent) {
        childMapper.deleteByParent(parent);
    }
}
